/******************************************************下拉框*****************************************************/
var hideComboPanel=function(comboId){
    var combo = _$.getById(comboId);
    var panel = $("#combo_panel_" + comboId);
    var combo_show = combo.combo_show;
    var multiple = combo.getOption("multiple");
    var speed = combo.getOption("speed");
    if(multiple || (isNotEmpty(combo.show_more) && combo.show_more.is(":visible"))){
        combo._hideMore();
    }

    combo_show.css({
        "border":"1px solid #ccc"
    });
    panel.slideUp(speed);

    if(!multiple) {
        combo.show_arrow.removeClass("icon_up").addClass("icon_down");
    }
};
var showComboPanel=function(comboId){

    var combo = _$.getById(comboId);
    var panel = $("#combo_panel_" + comboId);
    var multiple = combo.getOption("multiple");
    var speed = combo.getOption("speed");
    var combo_show = combo.combo_show;
    var panel_content = combo.panel_content;


    var panelWidth = combo.getOption("panelWidth");
    var panelHeight = combo.getOption("panelHeight");
    var realWidth,width,height;
    if(multiple){
        realWidth=combo_show.innerWidth();
    }else{
        realWidth=combo_show.innerWidth()-1;
    }
    panelWidth = isEmpty(panelWidth) || parseInt(panelWidth)<0 ? 0 : panelWidth;
    width  = panelWidth > realWidth?panelWidth:realWidth;
    width  = panelWidth < realWidth?realWidth:panelWidth;

    height =  isEmpty(panelHeight) || parseInt(panelHeight)<0 ? 200 : panelHeight;
    panel.width(width);
    panel_content.height(height);
    combo.setOption("panelHeight",height);

    $(".combo_panel").each(function(){
        var comboId2 = $(this).attr("comboId");
        hideComboPanel(comboId2);
    });
    combo_show.css({
        "border-bottom":"0px solid #ccc"
    });
    if(!multiple) {
        combo.show_arrow.removeClass("icon_down").addClass("icon_up");
    }
    panel.slideDown(speed);
    combo.panel_search.focus();
    combo.panel_search.val("");
    combo._searchByTxt("");
};

_$.Combo=function (target){
    var _this = this;
    _$.Combo.superclass.constructor.call(_this,target);
};
_$.regClass("Combo",_$.Combo);

_$.extend(_$.Combo,_$.FormItem, {
    _cls:_$._clsPre + "Combo",
    _uiCls:_$._uiPreCls + "Combo",
    _attrProps:function(){
        var _this = this;
        var properties = _$.Combo.superclass._attrProps.call(_this);
        properties.addAll(["onchange",{"multiple":"boolean","panelWidth":"number","panelHeight":"number"}]);
        return properties;
    },
    _attrOpts:function(){
        var _this = this;
        var opts = _$.Combo.superclass._attrOpts.call(_this);
        return $.extend(true,opts,{
            "multiple":false,
            "defaultText":"==请选择==",
            "speed":50
        });
    },
    _editModel:function(){
        var _this = this;
        _this._clearShow();

        var multiple = _this.getOption("multiple");
        if(!multiple){
            _this._createSingle();
        }else{
            _this._createMulti();
        }
        hideComboPanel(_this.id);
        _this._init();
        _this._bindEvents();
    },
    _createSingle:function(){
        var _this = this;

        var source='<div class="combo_single" >'+
            '<div class="combo_single_show">'+
            '<span type="text" class="combo_single_input"></span>' +
            '<i class="delete_icon"></i>'+
            '<i class="combo_single_arrow"></i>'+
            '</div> ' +
            '<div class="combo_panel" id="combo_panel_'+_this.id+'" comboId="'+_this.id+'">'+
            '<div class="combo_search">'+
            '<input type="text" class="combo_search_input"/>'+
            '<i class="combo_search_icon"></i>'+
            '</div>'+
            '<div class="combo_content"></div>'+
            '</div>'+
            '</div>';

        _this._replaceNode(source);
        _this.component = $(".combo_single",_this.el);
        _this.combo_show = $(".combo_single_show",_this.component);
        _this.combo_input = $(".combo_single_input",_this.component);
        _this.show_arrow = $(".combo_single_arrow",_this.component);
        _this.combo_delete = $(".delete_icon",_this.component);

        _this.panel = $(".combo_panel",_this.component);
        _this.panel_search = $(".combo_search_input",_this.panel);
        _this.panel_content = $(".combo_content",_this.panel);

        _this.component.attr("comboId",_this.id);
        _this.el.addClass(_this._uiCls);
        _this._setStyle();
        _this.setOption("model",_EDITABLE_);
        _this._initPanel();
        _this._init();
        hideComboPanel(_this.id);
        _this._bindSingleEvents();

    },
    _createMulti:function(){
        var _this = this;
        var source = '<div class="combo_multi">'+
        '<div class="combo_multi_show">'+
        '<div class="combo_multi_input"></div>'+
        '<i class="combo_multi_more_bar icon_right"></i>'+
        '<div class="combo_multi_more"></div>'+
        '</div>'+
        '<div class="combo_panel" id="combo_panel_'+_this.id+'" comboId="'+_this.id+'">'+
        '<div class="combo_search">'+
        '<input type="text" class="combo_search_input"/>'+
        '<i class="combo_search_icon"></i>'+
        '</div>'+
        '<div class="combo_content"></div></div>'+
        '</div>';

        _this._replaceNode(source);
        _this.component = $(".combo_multi",_this.el);
        _this.combo_show = $(".combo_multi_show",_this.component);
        _this.combo_input = $(".combo_multi_input",_this.component);
        _this.show_more_bar = $(".combo_multi_more_bar",_this.component);
        _this.show_more = $(".combo_multi_more",_this.component);

        _this.panel = $(".combo_panel",_this.component);
        _this.panel_search = $(".combo_search_input",_this.panel);
        _this.panel_content = $(".combo_content",_this.panel);

        _this.component.attr("comboId",_this.id);
        _this.el.addClass(_this._uiCls);
        _this._setStyle();
        _this.setOption("model",_EDITABLE_);
        _this._initPanel();
        _this._init();
        hideComboPanel(_this.id);
        _this._bindMultiEvents();

    },
    _bindSingleEvents:function(){
        var _this = this;
        var comboId = _this.id;
        _this.combo_show.click(function(e) {
            if(_this.panel.is(":visible")){
                hideComboPanel(comboId);
            }else{
                showComboPanel(comboId);
            }
            return false;
        });

        _this.combo_delete.click(function(){
            _this.setValue("");
            _this._searchByTxt("");
            return false;
        });
        $(document).click(function(e) {
            if($(e.target).closest("div.combo_panel").size()>0){
                return;
            }else{
                hideComboPanel( _this.id);
            }
        });
    },
    _bindMultiEvents:function(){
        var _this = this;
        var comboId = _this.id;
        _this.combo_input.click(function(e) {
            if(_this.panel.is(":visible")){
                hideComboPanel(comboId);
            }else{
                showComboPanel(comboId);
            }
            return false;
        });

        $(document).click(function(e) {
            if($(e.target).closest("div.combo_panel").size()>0 || $(e.target).closest("div.combo_multi_more").size()>0){

                return;
            }else{
                hideComboPanel( _this.id);
            }
        });
        _this.show_more_bar.click(function(){
            if(_this.show_more.is(":visible")){
                _this._hideMore();
            }else{
                _this._showMore();
            }
            return false;
        });
    },
    _hideMore:function(){
    },
    _showMore:function(){
    },
    _initPanel:function(){
    },
    _clearSingle:function(){
        var _this = this;
        if( _this.show_arrow){
            _this.show_arrow.unbind();
            _this.show_arrow.remove();
        }
        if( _this.combo_input){
            _this.combo_input.unbind();
            _this.combo_input.remove();
        }
        if( _this.combo_show){
            _this.combo_show.unbind();
            _this.combo_show.remove();
        }
        _this._clearPanel();
    },
    _clearMulti:function(){
        var _this = this;
        if( _this.show_more_bar){
            _this.show_more_bar.unbind();
            _this.show_more_bar.remove();
        }
        if( _this.show_more){
            _this.show_more.unbind();
            _this.show_more.remove();
        }
        if( _this.combo_input){
            _this.combo_input.unbind();
            _this.combo_input.remove();
        }
        if( _this.combo_show){
            _this.combo_show.unbind();
            _this.combo_show.remove();
        }
        _this._clearPanel();
    },
    _bindPanelEvents:function(){
        var _this = this;
        _this.panel_search.bind("input",function(e){
            var searchTxt = $(this).val();
            _this._searchByTxt(searchTxt);
            return false;
        });
    },
    _searchByTxt:function(searchTxt){
    },
    _setStyle:function(){
        var _this = this;
        var width = _this.getOption('width');


        if(isNotEmpty(width)){
            _this.combo_show.css({
                "width":width + "px"
            });
        }
        var height =_this.getOption('height');
        if(isNotEmpty(height)){
            _this.combo_show.css({
                "height":height + "px"
            });
        }
    }
});
